package com.googlecode.agscrum.model.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.NotNull;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Configurable;

import com.googlecode.agscrum.model.exceptions.GScrumDomainException;
import com.googlecode.agscrum.model.exceptions.MessageFactory;
import com.googlecode.agscrum.model.exceptions.GScrumDomainException.TypeMessage;

@Entity
@SequenceGenerator(name = "sprint_sequence", sequenceName = "sprint_sequence")
@Configurable(autowire = Autowire.BY_NAME)
public class Sprint {
	@Id
	@GeneratedValue(generator = "sprint_sequence", strategy = GenerationType.SEQUENCE)
	@Column(name = "id_sprint")
	private Long id;
	@Column(nullable = false, length = 20)
	@NotNull(message = "sprint.description.required")
	@NotEmpty(message = "sprint.description.required")
	private String description;
	@Column(nullable = false, length = 1000)
	@NotNull(message = "sprint.goal.required")
	@NotEmpty(message = "sprint.goal.required")
	private String goal;
	@Temporal(TemporalType.DATE)
	@Column(nullable = false)
	@NotNull(message = "sprint.initialDate.required")
	private Date initialDate;
	@Temporal(TemporalType.DATE)
	@Column(nullable = false)
	@NotNull(message = "sprint.finalDate.required")
	private Date finalDate;
	@ManyToOne
	@JoinColumn(name = "id_product", nullable = false)
	private Product product;
	@OneToMany(mappedBy = "sprint")
	@JoinColumn(name = "id_sprint")
	private List<Item> items;
	@ManyToOne
	@JoinColumn(name = "id_team", nullable = false)
	@NotNull(message = "sprint.team.required")
	private Team team;
	@Transient
	private MessageFactory messageFactory;

	public void setMessageFactory(MessageFactory messageFactory) {
		this.messageFactory = messageFactory;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Product getProduct() {
		return product;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setGoal(String goal) {
		this.goal = goal;
	}

	public String getGoal() {
		return goal;
	}

	public void setInitialDate(Date initialDate) {
		this.initialDate = initialDate;
	}

	public Date getInitialDate() {
		return initialDate;
	}

	public void setFinalDate(Date finalDate) {
		this.finalDate = finalDate;
	}

	public Date getFinalDate() {
		return finalDate;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public Team getTeam() {
		return team;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}

	public List<Item> getItems() {
		return items;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void saveItem(Item item) throws GScrumDomainException {
		if (item.hasEstimate()) {
			if (items == null) {
				items = new ArrayList<Item>();
			}

			if (items.contains(item)) {
				items.set(items.indexOf(item), item);
			} else {
				item.setSprint(this);
				items.add(item);
			}
		} else {
			throw new GScrumDomainException(messageFactory.createMessage(
					"sprint.estimate.required", null, TypeMessage.ERROR));
		}
	}

	public void removeItem(Item item) {
		if (items != null) {
			if (items.contains(item)) {
				items.remove(item);
				item.setSprint(null);
			}
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Sprint other = (Sprint) obj;
		if (getId() == null) {
			if (other.getId() != null)
				return false;
		} else if (!getId().equals(other.getId()))
			return false;
		return true;
	}

	public boolean isRemoved() {
		if (items != null) {
			for (Item item : items) {
				if (item.isDone()) {
					return false;
				}
			}
		}
		return true;
	}
}
